using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ComputerShopE3.Models;

namespace ComputerShopE3.Controllers
{
    public class FavoriteController : Controller
    {
        //
        // GET: /Favorite/
        ComputerShopDBEntities1 _db = new ComputerShopDBEntities1();

        [SpiderE3Authorize(Roles = SiteRoles.Admin | SiteRoles.Employee | SiteRoles.Member)]
        public ActionResult Index()
        {
            var dssp = _db.SAN_PHAM_UA_THICH.Where(c => c.NGUOI_DUNG.TenDangNhap == User.Identity.Name);

            foreach (var sp in dssp)
            {
                sp.SAN_PHAMReference.Load();
                sp.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
            }

            return View(dssp.ToList());
        }

        //
        // GET: /Favorite/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Favorite/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Favorite/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Favorite/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Favorite/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult AddToFavorite(int id)
        {
            try
            {
                SAN_PHAM sp = _db.SAN_PHAM.First(c => c.id == id);
                NGUOI_DUNG nd = _db.NGUOI_DUNG.First(c => c.TenDangNhap == User.Identity.Name);

                SAN_PHAM_UA_THICH sput = new SAN_PHAM_UA_THICH
                {
                    SAN_PHAM = sp,
                    NGUOI_DUNG = nd
                };

                _db.AddToSAN_PHAM_UA_THICH(sput);
                _db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                // TODO: Add update logic here
                _db.DeleteObject(_db.SAN_PHAM_UA_THICH.First(c => c.id == id));
                _db.SaveChanges();
            }
            catch
            {
            }

            return RedirectToAction("Index");
        }
    }
}
